package cn.sinata.wanglin.mine.activity;
import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import com.xilada.xldutils.activitys.RecyclerListActivity;
import com.xilada.xldutils.utils.DensityUtil;
import com.xilada.xldutils.utils.Utils;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;
import java.util.ArrayList;
import java.util.List;
import cn.sinata.wanglin.R;
import cn.sinata.wanglin.adapter.SayingAndBaguaAdapter;
import cn.sinata.wanglin.model.Makecomplaints;
import cn.sinata.wanglin.net.HttpManager;
import cn.sinata.wanglin.net.model.util.ResultDataSubscriber;

public class MoreSayingAndBaguaActivity extends RecyclerListActivity {

    private List<Makecomplaints> mMakecomplaints=new ArrayList<Makecomplaints>();
    private SayingAndBaguaAdapter mSayingAndBaguaAdapter;
    private int pageNo = 0;
    private int pageSize = 10;
    private int type;
    private String userid;
    @Override
    protected RecyclerView.Adapter setAdapter() {
        if(mSayingAndBaguaAdapter==null){
            type=getIntent().getIntExtra("type",1);
            mSayingAndBaguaAdapter=new SayingAndBaguaAdapter(mMakecomplaints,MoreSayingAndBaguaActivity.this,type);
        }
        return mSayingAndBaguaAdapter;
    }

    @Override
    protected void pullDownRefresh() {
        super.pullDownRefresh();
        pageNo = 0;
        getData();
    }

    @Override
    protected void loadMore() {
        super.loadMore();
        pageNo++;
        getData();
    }

    @Override
    protected void initView() {
        super.initView();
        type=getIntent().getIntExtra("type",1);
        userid=getIntent().getStringExtra("userid");
        HorizontalDividerItemDecoration itemDecoration = new HorizontalDividerItemDecoration.Builder(mContext)
                .size(DensityUtil.dip2px(mContext,10))
                .color(ContextCompat.getColor(mContext, R.color.dividing_line_color_wanglin))
                .build();
        setItemDecoration(itemDecoration);
        //1 吐槽 2八卦
        switch (type){
            case 1:
               setTitle("小区吐槽");
                break;
            case 2:
                setTitle("同城八卦");
                break;
        }
        showDialog();
        getData();
    }
    /**
     * 吐槽
     */
    private void getData() {
        HttpManager.getMyZone(userid,type,pageNo,pageSize).subscribe(new ResultDataSubscriber<ArrayList<Makecomplaints>>(this) {
            @Override
            public void onSuccess(String msg, ArrayList<Makecomplaints> data) {
                if (pageNo == 0) {
                    mMakecomplaints.clear();
                }
                if (data.size() == 0) {
                    if (pageNo == 0) {
                        setLoadMoreText("暂无数据");
                    } else {
                        pageNo --;
                        setLoadMoreText("没有更多了");
                    }
                }
                mMakecomplaints.addAll(data);
                mSayingAndBaguaAdapter.notifyDataSetChanged();
            }
        });
    }
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Utils.systemErr("=resultCod-->" + resultCode);
        Utils.systemErr("requestCode-->" + requestCode);
        if(resultCode==-1){
            switch (requestCode)
            {
                case 12:
                    showDialog("刷新中...");
                    pullDownRefresh();
                    break;
            }

        }
    }
}
